package com.example.demo;
 
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
 
public class WordExportController2 {
	
	private static final Integer F_WIDTH = 420;
	
	private static final Integer S_WIDTH = 4000;
	
	private static final Integer T_WIDTH = 450;
	
	private static final Integer HEIGHT = 460;
 
    public static void main(String[] args)throws Exception {
        //Blank Document
        XWPFDocument document= new XWPFDocument();
        List<XWPFTable> tables = document.getTables();
        //Write the Document in file system
        FileOutputStream out = new FileOutputStream(new File("create_table.docx"));
 
 
        //添加标题
        XWPFParagraph titleParagraph = document.createParagraph();
        //设置段落居中
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
 
        XWPFRun titleParagraphRun = titleParagraph.createRun();
        titleParagraphRun.setText("XXXX维保工作报告");
        titleParagraphRun.setColor("000000");
        titleParagraphRun.setFontSize(20);
        titleParagraphRun.setBold(true);
        // titleParagraphRun.setUnderline(UnderlinePatterns.SINGLE);
 
        //换行
        createParagraph(document,null,null,null,null);
 
 
        //基本信息表格
        XWPFTable infoTable = document.createTable();
        //去表格边框
        infoTable.getCTTbl().getTblPr().unsetTblBorders();
 
 
        //列宽自动分割
        CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
        infoTableWidth.setType(STTblWidth.DXA);
        infoTableWidth.setW(BigInteger.valueOf(9072));
 
 
        //表格第一行
        XWPFTableRow infoTableRowOne = infoTable.getRow(0);
        infoTableRowOne.getCell(0).setText("报告名称");
        infoTableRowOne.addNewTableCell().setText("XXXX维保工作报告");
 
        //表格第二行
        XWPFTableRow infoTableRowTwo = infoTable.createRow();
        infoTableRowTwo.getCell(0).setText("位置");
        infoTableRowTwo.getCell(1).setText("XXXX区域位置");
 
        //表格第三行
        XWPFTableRow infoTableRowThree = infoTable.createRow();
        infoTableRowThree.getCell(0).setText("紧急呼叫");
        infoTableRowThree.getCell(1).setText("18888888888");
 
        //表格第四行
        XWPFTableRow infoTableRowFour = infoTable.createRow();
        infoTableRowFour.getCell(0).setText("维保时间");
        infoTableRowFour.getCell(1).setText("2018-11-13");
 
        //表格第五行
        XWPFTableRow infoTableRowFive = infoTable.createRow();
        infoTableRowFive.getCell(0).setText("维保人");
        infoTableRowFive.getCell(1).setText("XXXX用户");
        
        //表格第五行
        infoTableRowFive = infoTable.createRow();
        infoTableRowFive.getCell(0).setText("维保周期");
        infoTableRowFive.getCell(1).setText("月检");
 
 
        //两个表格之间加个换行
        createParagraph(document,null,null,null,null);
        
        //段落
        XWPFParagraph firstParagraph = document.createParagraph();
        XWPFRun run = firstParagraph.createRun();
        run.setText("第一部分：检查系统");
        run.setColor("000000");
        run.setFontSize(16);
        run.setBold(true);
        
        createParagraph(document,null,null,null,null);
        createParagraph(document,"火灾自动报警系统:",14,true,null);
        
        //测试表格
        XWPFTable testTable = document.createTable();
        //列宽自动分割
        CTTblWidth comTableWidth = testTable.getCTTbl().addNewTblPr().addNewTblW();
        comTableWidth.setType(STTblWidth.DXA);
        comTableWidth.setW(BigInteger.valueOf(9072));
        
        List<String> list = getData();
        int row = list.size();
        int col = 7;
        XWPFTableRow comTableRowTwo;
        for(int i=0;i<row;i++) {
        	if(i==0) {
        		//表格第一行
                comTableRowTwo = testTable.getRow(0);
                comTableRowTwo.getCell(0).setText("火灾自动报警系统");
                if(i<=(row-7)) {
                	comTableRowTwo.addNewTableCell().setText("火灾报警控制盘");
                }else {
                	comTableRowTwo.addNewTableCell().setText("现场报警回路及设备");
                }
                comTableRowTwo.addNewTableCell().setText(list.get(i));
                comTableRowTwo.addNewTableCell().setText("");
                comTableRowTwo.addNewTableCell().setText("");
                comTableRowTwo.addNewTableCell().setText("");
                comTableRowTwo.addNewTableCell().setText("");
                /*setCellWidth(comTableRowTwo.getCell(2), S_WIDTH);
                setCellWidth(comTableRowTwo.getCell(1), F_WIDTH);
                setCellWidth(comTableRowTwo.getCell(0), F_WIDTH);
                setCellCenterBatch(comTableRowTwo, 7);
                setRowHeight(comTableRowTwo, HEIGHT);*/
        	}else {
        		comTableRowTwo = testTable.createRow();
                comTableRowTwo.getCell(0).setText("火灾自动报警系统");
                if(i<(row-6)) {
                	comTableRowTwo.getCell(1).setText("火灾报警控制盘");
                }else {
                	comTableRowTwo.getCell(1).setText("现场报警回路及设备");
                }
                comTableRowTwo.getCell(2).setText(list.get(i));
                comTableRowTwo.getCell(3).setText("");
                comTableRowTwo.getCell(4).setText("");
                comTableRowTwo.getCell(5).setText("");
                comTableRowTwo.getCell(6).setText("");
                /*setCellWidth(comTableRowTwo.getCell(3), T_WIDTH);
                setCellWidth(comTableRowTwo.getCell(2), S_WIDTH);
                setCellWidth(comTableRowTwo.getCell(1), F_WIDTH);
                setCellWidth(comTableRowTwo.getCell(0), F_WIDTH);
                setCellCenterBatch(comTableRowTwo, col);
                setRowHeight(comTableRowTwo, HEIGHT);*/
        	}
        	setCellWidth(comTableRowTwo.getCell(3), T_WIDTH);
            setCellWidth(comTableRowTwo.getCell(2), S_WIDTH);
            setCellWidth(comTableRowTwo.getCell(1), F_WIDTH);
            setCellWidth(comTableRowTwo.getCell(0), F_WIDTH);
            setCellCenterBatch(comTableRowTwo, col);
            setRowHeight(comTableRowTwo, HEIGHT);
        	mergeCellsHorizontal(testTable, i, col-3, col-1);
        	if(i%2==0) {
        		comTableRowTwo.getCell(3).setText("良好");
        	}else {
        		comTableRowTwo.getCell(3).setText("损坏");
        	}
        	comTableRowTwo.getCell(4).setText("备注备注备注备注备注备");
        }
        mergeCellsVertically(testTable, 0, 0, row-1);
        mergeCellsVertically(testTable, 1, 0, row-6);
        mergeCellsVertically(testTable, 1, row-6, row-1);
        
        createParagraph(document, null, null, null,null);
        createParagraph(document, null, null, null,null);
        createParagraph(document, "维保单位报告人 ____________         日期 ____________", null, null,ParagraphAlignment.CENTER);
        createParagraph(document, null, null, null,null);
        createParagraph(document, "物业管理确认人 ____________         日期 ____________", null, null,ParagraphAlignment.CENTER);
        createParagraph(document, null, null, null,null);
        createParagraph(document, "    甲方确认人 ____________         日期 ____________", null, null,ParagraphAlignment.CENTER);
        
        
        //基本信息表格
        infoTable = document.createTable();
        //去表格边框
        infoTable.getCTTbl().getTblPr().unsetTblBorders();
        infoTable.getCTTbl().addNewTblPr().addNewJc().setVal(STJc.CENTER);
        //列宽自动分割
        infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
        infoTableWidth.setType(STTblWidth.DXA);
        infoTableWidth.setW(BigInteger.valueOf(9072));
        //表格第一行
        infoTableRowOne = infoTable.getRow(0);
        infoTableRowOne.getCell(0).setText("维保单位报告人");
        infoTableRowOne.addNewTableCell().setText("___________");
        infoTableRowOne.addNewTableCell().setText("日期");
        infoTableRowOne.addNewTableCell().setText("___________");
        
        
        infoTableRowOne = infoTable.createRow();
        infoTableRowOne.getCell(0).setText("物业管理确认人");
        infoTableRowOne.getCell(1).setText("___________");
        infoTableRowOne.getCell(2).setText("日期");
        infoTableRowOne.getCell(3).setText("___________");
        
        infoTableRowOne = infoTable.createRow();
        infoTableRowOne.getCell(0).setText("    甲方确认人");
        infoTableRowOne.getCell(1).setText("___________");
        infoTableRowOne.getCell(2).setText("日期");
        infoTableRowOne.getCell(3).setText("___________");
        
        CTP ctpHeader1 = CTP.Factory.newInstance();
        CTR ctrHeader1 = ctpHeader1.addNewR();
        CTText ctHeader1 = ctrHeader1.addNewT();
        ctHeader1.setStringValue("");
        XWPFParagraph headerParagraph1 = new XWPFParagraph(ctpHeader1, document);
        XWPFRun createRun1 = headerParagraph1.createRun();
        createRun1.setText("维保单位报告人 ");
        XWPFParagraph headerParagraph2 = new XWPFParagraph(ctpHeader1, document);
        XWPFRun createRun2 = headerParagraph2.createRun();
        createRun2.setText("               ");
        createRun2.setUnderline(UnderlinePatterns.SINGLE);
        
        CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
        XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
 
        //添加页眉
        CTP ctpHeader = CTP.Factory.newInstance();
        CTR ctrHeader = ctpHeader.addNewR();
        CTText ctHeader = ctrHeader.addNewT();
        String headerText = "Java POI create MS word file.";
        ctHeader.setStringValue(headerText);
        XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
        //设置为右对齐
        headerParagraph.setAlignment(ParagraphAlignment.RIGHT);
        XWPFParagraph[] parsHeader = new XWPFParagraph[1];
        parsHeader[0] = headerParagraph;
        policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
 
        //添加页脚
        CTP ctpFooter = CTP.Factory.newInstance();
        CTR ctrFooter = ctpFooter.addNewR();
        CTText ctFooter = ctrFooter.addNewT();
        String footerText = "http://blog.csdn.net/zhouseawater";
        ctFooter.setStringValue(footerText);
        XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);
        headerParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFParagraph[] parsFooter = new XWPFParagraph[1];
        parsFooter[0] = footerParagraph;
        policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
 
 
        document.write(out);
        out.close();
        System.out.println("create_table document written success.");
    }
    
    /**
     * 
     * @Description: word跨列合并单元格
     * @param table
     * @param row
     * @param fromCell
     * @param toCell
     */
 	public static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {  
         for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {  
             XWPFTableCell cell = table.getRow(row).getCell(cellIndex);  
             if ( cellIndex == fromCell ) {  
                 // The first merged cell is set with RESTART merge value  
                 cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);  
             } else {  
                 // Cells which join (merge) the first one, are set with CONTINUE  
                 cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);  
             }  
         }  
     }  
 	
 	/**
 	 * 
 	 * @Description: word跨行并单元格
 	 * @param table
 	 * @param col
 	 * @param fromRow
 	 * @param toRow
 	 */
 	public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {  
         for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {  
             XWPFTableCell cell = table.getRow(rowIndex).getCell(col);  
             if ( rowIndex == fromRow ) {  
                 // The first merged cell is set with RESTART merge value  
                 cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);  
             } else {  
                 // Cells which join (merge) the first one, are set with CONTINUE  
                 cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);  
             }  
         }  
     }
 	
 	/**
 	 * 
 	 * @Description: 设置单元格垂直水平居中
 	 * @param cell
 	 */
 	public static void setCellCenter(XWPFTableCell cell) {
        cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); //垂直居中
        CTTc cttc = cell.getCTTc();
        CTTcPr ctPr = cttc.addNewTcPr();
        ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
        cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
 	}
 	
 	/**
 	 * 
 	 * @Description: 批量设置单元格垂直居中
 	 * @param row
 	 * @param cellNum
 	 */
  	public static void setCellCenterBatch(XWPFTableRow row , Integer cellNum) {
  		for(int i=0;i<cellNum;i++) {
  			if(i==0 || i==1) {
  				row.getCell(i).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); //垂直居中
  		        CTTc cttc = row.getCell(i).getCTTc();
  		        CTTcPr ctPr = cttc.addNewTcPr();
  		        ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
  		        cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
  			}else {
  				row.getCell(i).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); //垂直居中
  			}
  		}
  	}
 	
  	/**
  	 * 
  	 * @Description: 设置单元格宽度
  	 * @param cell
  	 * @param width
  	 */
  	public static void setCellWidth(XWPFTableCell cell,Integer width) {
  		cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(width));
  	}
  	
  	/**
  	 * 
  	 * @Description: 设置行高度
  	 * @param row
  	 * @param height
  	 */
  	public static void setRowHeight(XWPFTableRow row,Integer height) {
  		row.setHeight(height);
  	}
  	
  	/**
  	 * 
  	 * @param document 文档对象
  	 * @param content 段落内容
  	 * @param size 字体大小
  	 * @param value 是否加粗
  	 * @param align 对齐方式
  	 */
  	public static void createParagraph(XWPFDocument document,String content,Integer size,Boolean value,ParagraphAlignment align) {
  		XWPFParagraph paragraph = document.createParagraph();
  		if(align!=null) {
  			paragraph.setAlignment(align);
        }
  		XWPFRun paragraphRun = paragraph.createRun();
  		if(content!=null) {
	        paragraphRun.setText(content);
	        if(size!=null) {
	        	paragraphRun.setFontSize(size);
	        }
	        if(value!=null) {
	        	paragraphRun.setBold(value);
	        }
  		}else {
  			paragraphRun.setText("");
  		}
        
  	}
  	
  	public static List<String> getData() {
  		List<String> list = new LinkedList<>();
  		list.add("面板各功能表显示输入输出功能");
  		list.add("面板各功能键动作");
  		list.add("报警、输出回路、断线故障声光显示功能");
  		list.add("备用电源断电、故障声光显示功能");
  		list.add("主音箱、地区音响、故障报警隔断功能");
  		list.add("主机报警、显示联动列印功能");
  		list.add("主机联动隔断功能");
  		list.add("主机与手动按钮通话功能");
  		list.add("主机报警输出功能");
  		list.add("各楼层温感探测器及信号回路");
  		list.add("各楼层煤气探测器及信号回路");
  		list.add("各楼层烟感探测器及信号回路");
  		list.add("各楼层手动按钮报警器及信号回路");
  		list.add("各楼层警铃及信号回路");
  		list.add("各联动模组及控制回路");
  		return list;
  	}
 
}